﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TGSPR
    {

        public TGSPR()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGSDAT();
            DataContext = MV;
            InitializeComponent();
        }
        public override void QueryCommand(object sender, RoutedEventArgs e)
        {
            dwData = TBMain.NewRow();
            dwData["GSMANDANT"] = app.Mandant;
            dwData["GSART"] = " AND GSART LIKE '%" + ART.Trim() + "%'";
            dwData["GSHANDLNR"] = " AND GSHANDLNR LIKE '%" + DES.Trim() + "%'";
            
            SResult = TMV.QGS08(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }
        public override void ImportExcel(object sender, RoutedEventArgs e)
        {
            if (cDR == null)
                return;
            try
            {
                cDR.DocType = "LOADEXCEL";
                // cDR.Root = @"C:\IntexReport\Data\KD\EXCEL"; 
                cDR.DESTFOLDER = @"C:\IntexReport\Data\GS\EXCEL";
                if (!System.IO.Directory.Exists(cDR.DESTFOLDER))
                {
                    throw new Exception(cDR.DESTFOLDER + " 不存在");
                }
                cDR.PRINT(null);
                SResult = cDR.SetExchange;
                if (SResult.Tables.Count > 0)
                {
                    ExGS03(SResult);
                    ExGS07(SResult); 
                }
                else
                {
                    MessageBox.Show("确认存在" + @"C:\IntexReport\Data\GS\EXCEL\ITXEX-XXXX.xls");
                }
                QueryCommand(null, null);
                vwMaster.View.MoveCurrentToFirst();
                while (true)
                {
                    vwMaster.View.MoveCurrentToNext();
                    if (vwMaster.View.IsCurrentAfterLast)
                        break;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {

            }
        }
        public void ExGS03(DataSet db)
        {
            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = Helper.ConvertGB2312To8859P1(row["GSART"].ToString());
                string des = Helper.ConvertGB2312To8859P1(row["GSHANDLNR"].ToString());
                string MART = row["GSNUMART"].ToString();
                string NNR = row["GSGARNNR"].ToString();
                string ACH = row["GSFACH"].ToString();
                string NUM = row["GSRECHNUM"].ToString(); 
                dwData["GSMANDANT"] = app.Mandant;
                dwData["GSART"] = Helper.Left(art.PadRight(20), 20);
                dwData["GSVAR"] = "".PadRight(3);
                dwData["GSHANDLNR"] = Helper.Left(des.PadRight(60), 60);
                dwData["GSNUMART"] =Helper.Left(MART.PadRight(4),4);
                dwData["GSGARNNR"] = double.Parse(NNR) * 10000;
                dwData["GSFACH"] = Helper.Left(ACH.PadRight(3), 3);
                dwData["GSRECHNUM"] = Helper.Left(MART.PadRight(4), 4);
                dwData["GSRECHNR"] = double.Parse(NUM) * 10000;
                TMV.DGS01(dwData);
                TMV.BATCHWB14(dwData);
            }
            TMV.POSTCHG();
        }
        public void ExGS07(DataSet db)
        {

            INFO = db.Tables[0].Rows.Count.ToString() + "行数据已经读入";
            dwData =TMV.VGSC.TBMain.NewRow();
            TMV.VGSC.SetUpNewRow(ref dwData);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                string art = Helper.ConvertGB2312To8859P1(row["GSART"].ToString()); 
                dwData["GSCMANDANT"] = app.Mandant;
                dwData["GSCART"] = Helper.Left(art.PadRight(20), 20);
                dwData["GSCVAR"] = "".PadRight(3);
                dwData["GSCFARBE"] = "".PadRight(10);
                TMV.VGSC.DGSC01(dwData);
                TMV.VGSC.BATCHWB14(dwData);
            }
            TMV.VGSC.POSTCHG();
        }

    }
}
